﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

namespace SimpleBomberman
{
    public partial class ResultsForm : Form
    {
        private int time;
        private int point;
        private int index;
        private string[,] matrix;

        public ResultsForm()
        {
            InitializeComponent();
        }

        public ResultsForm(int time, int point, int index)
        {
            InitializeComponent();

            this.time = time;
            this.point = point;
            this.index = index;
        }

        private void LoadRecords()
        {
            try
            {
                string direction = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                string FullFileName = Path.Combine(direction, "records.txt");

                using (StreamReader sr = new StreamReader(FullFileName))
                {
                    string line = sr.ReadToEnd();
                    string[] stringArray = line.Split(' ');
                    matrix = new string[stringArray.Length / 3, 3];
                    int k = 0;
                    for (int i = 0; i < stringArray.Length / 3; i++)
                    {
                        for (int j = 0; j < 3; j++)
                        {
                            matrix[i, j] = stringArray[k];
                            k++;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
        }
        private void SaveRecords()
        {
            try
            {
                string direction = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                string FullFileName = Path.Combine(direction, "records.txt");

                using (StreamWriter sw = new StreamWriter(FullFileName))
                {
                    for (int i = 0; i < matrix.GetLength(0); i++)
                    {
                        for (int j = 0; j < matrix.GetLength(1); j++)
                        {
                            if (i == matrix.GetLength(0) - 1 && j == matrix.GetLength(1) - 1)
                            {
                                sw.Write(matrix[i, j]);
                            }
                            else
                            {
                                sw.Write(matrix[i, j] + " ");
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("The file could not be write:");
                Console.WriteLine(e.Message);
            }
        }
        private void ShowRecords()
        {
            dataGridView1.RowCount = this.matrix.GetLength(0);
            dataGridView1.ColumnCount = this.matrix.GetLength(1) + 1;

            dataGridView1.Columns[0].HeaderText = "Level";
            dataGridView1.Columns[1].HeaderText = "Time";
            dataGridView1.Columns[2].HeaderText = "Points";
            dataGridView1.Columns[3].HeaderText = "Name";

            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                dataGridView1.Columns[i].Width = 70;
            }

            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    dataGridView1.Rows[i].Cells[j + 1].Value = matrix[i, j].ToString();
                }
                dataGridView1.Rows[i].Cells[0].Value = (i + 1).ToString();
            }
        }

        private void CheckRecords()
        {
            if (int.Parse(dataGridView1.Rows[this.index].Cells[1].Value.ToString()) > this.time)
            {
                label5.Text = "You have a new record!";
            }
            else
            {
                label5.Text = "Your time isn't perfect :(";
                label6.Visible = false;
                textBox1.Visible = false;
                button1.Visible = false;
            }
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            this.label2.Text = this.time.ToString();
            this.label4.Text = this.point.ToString();
            this.label7.Text = "Level: " + (this.index + 1).ToString();
            LoadRecords();
            ShowRecords();
            CheckRecords();
            dataGridView1.ClearSelection();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                this.matrix[this.index, 0] = this.time.ToString();
                this.matrix[this.index, 1] = this.point.ToString();
                this.matrix[this.index, 2] = textBox1.Text;

                SaveRecords();
                LoadRecords();
                ShowRecords();
            }
            else
            {
                MessageBox.Show("Enter your Name!");
            }
            dataGridView1.ClearSelection();
        }
    }
}
